﻿using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using System.Diagnostics;
using System.Windows.Forms;

#if WINDOWS
using Window = Microsoft.UI.Xaml.Window;
#endif

namespace ymk_camera_surveillance
{
    public class ChangeShellTitleMsg : CommunityToolkit.Mvvm.Messaging.Messages.ValueChangedMessage<string>
    {
        public ChangeShellTitleMsg(string title) : base(title)
        {
        }
    }

    public class ChangeFullScreenMsg : CommunityToolkit.Mvvm.Messaging.Messages.ValueChangedMessage<string>
    {
        public ChangeFullScreenMsg(string title) : base(title)
        {
        }
    }

    public partial class AppShell : Shell
    {
        public static string ShellTitle = "悦码监控系统";

        public AppShell()
        {
            InitializeComponent();

            Routing.RegisterRoute(nameof(NotePage), typeof(NotePage));

            this.Title = ShellTitle;

            Debug.WriteLine("程序运行目录： " + System.AppDomain.CurrentDomain.BaseDirectory);
            //  程序运行目录D:\git_proj\ymk_camera_surveillance\bin\Debug\net7.0 - windows10.0.19041.0\win10 - x64\AppX\

            Debug.WriteLine("程序数据目录： " + FileSystem.AppDataDirectory);
            //C: \Users\limengkai\AppData\Local\Packages\d19bf10f - e349 - 45f0 - 99d3 - f3308bf2864d_df70bnyb6p55e\LocalState

            InitMessager();
            this.FlyoutBehavior = FlyoutBehavior.Disabled;
        }

        private void InitMessager()
        {
            App.KB_HOOK.MouseMoved += KB_HOOK_MouseMoved;

            WeakReferenceMessenger.Default
            .Register<AppShell, ChangeTabBarMessage>(
                this,
                async (recipient, message) =>
                {
                    //Debug.WriteLine("接收到 消息" + message.Value);
                    if (message.Value == 0)
                    {
                        await MainThread.InvokeOnMainThreadAsync(() =>
                        {

                            var xamlWindow = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView) as Microsoft.UI.Xaml.Window;
                            var currentWindow = GetAppWindowForCurrentWindow(xamlWindow);
                            xamlWindow.ExtendsContentIntoTitleBar = false;
                            currentWindow.SetPresenter(AppWindowPresenterKind.FullScreen);

                            //隐藏导航页
                            SetNavBarIsVisible(this, false);
                            SetTabBarIsVisible(this, false);
                            this.Title = "";
                        });
                    }
                    else
                    {
                        await MainThread.InvokeOnMainThreadAsync(() =>
                        {

                            var xamlWindow = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView) as Microsoft.UI.Xaml.Window;
                            var currentWindow = GetAppWindowForCurrentWindow(xamlWindow);
                            xamlWindow.ExtendsContentIntoTitleBar = true;
                            currentWindow.SetPresenter(AppWindowPresenterKind.Default);

                            //显示导航页
                            SetNavBarIsVisible(this, true);
                            SetTabBarIsVisible(this, true);

                            //https://learn.microsoft.com/en-us/answers/questions/1255949/how-can-i-customize-my-navbar-shell-in-maui
                            this.Title = ShellTitle;
                        });

                    }
                });

            WeakReferenceMessenger.Default
            .Register<AppShell, ChangeShellTitleMsg>(
                this,
                async (recipient, message) =>
                {
                    await MainThread.InvokeOnMainThreadAsync(() =>
                    {
                        this.Title = ShellTitle + "-" + message.Value;
                    });
                });


        }

        private async void KB_HOOK_MouseMoved(object sender, SharpHook.MouseHookEventArgs e)
        {
            if (e.Data.X < 5 && e.Data.Y > 100)
            {
                Debug.WriteLine($"edge {e.Data.X}");
                try
                {
                    await MainThread.InvokeOnMainThreadAsync(() =>
                    {
                        this.FlyoutIsPresented = true;
                    });
                }
                catch (Exception)
                {
                }
            }
            else if (e.Data.X > 400)
            {
                try
                {
                    await MainThread.InvokeOnMainThreadAsync(() =>
                    {
                        this.FlyoutIsPresented = false;
                    });
                }
                catch (Exception)
                {
                }
            }
        }

#if WINDOWS
        private AppWindow GetAppWindowForCurrentWindow(Window xamlWindow)
        {
            xamlWindow.ExtendsContentIntoTitleBar = false;

            var currentHwnd = ((MauiWinUIWindow)App.Current.Windows[0].Handler.PlatformView).WindowHandle;
            WindowId currentWid = Win32Interop.GetWindowIdFromWindow(currentHwnd);
            return AppWindow.GetFromWindowId(currentWid);
        }
#endif
    }

}